import urllib.request
import re
import base64


# 获取网页源码
def get_html():
    url = r'https://doub.bid/sszhfx/'
    request = urllib.request.Request(url)
    request.add_header('user-agent', 'Mozilla/5.0')
    response = urllib.request.urlopen(request)
    html = response.read()
    mystr = html.decode("utf8")
    response.close()
    return mystr


# 提取网页中的SSR账号
def get_ssr():
    str = get_html()
    data = re.findall(r"(ssr://[A-Z][\w]*)", str)
    return data


# 生成ssr.txt文件,每个SSR节点占一行
def build_file():
    data = get_ssr()
    f = open('../../wwwroot/wordpress/ssr/ssr.txt', 'w')
    for i in range(len(data)):
        f.write(data[i]+"\r\n")
    f.close()


# 对ssr.txt文件进行base64编码,生成ssr_base64.txt
def base64_code():
    fr = open('../../wwwroot/wordpress/ssr/ssr.txt', 'rb')
    fw = open('../../wwwroot/wordpress/ssr/ssr_base64.txt', 'wb')
    base64.encode(fr, fw)
    fr.close()
    fw.close()


# 获取网页SSR节点,生成ssr.txt和订阅服务器需要的文件ssr_base64.txt
if __name__ == '__main__':
    print("running...")
    build_file()
    base64_code()
    print("ok")
